package com.tbit.uqbike.client.controller;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.newEntity.AccountDiscount;
import com.tbit.uqbike.client.pojo.newEntity.UserAuthLog;
import com.tbit.uqbike.client.pojo.newEntity.UserDiscount;
import com.tbit.uqbike.client.service.SpecialAuthService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.service.UserService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 特殊认证
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 11:48 2022/9/16
 */
@Controller
@RequestMapping("/specialAuth")
@Slf4j
public class SpecialAuthController {

    @Autowired
    private SpecialAuthService specialAuthService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserService userService;

    /**
     * 查询区域特殊认证配置
     *
     * @param adAccountId 区域id
     * @param session
     * @param request
     * @param response
     */
    @RequestMapping("/getByAdAccountId")
    public void getByAdAccountId(String token, Integer adAccountId, HttpSession session,
                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(adAccountId, "-10001", "平台id不能为空");
            List<AccountDiscount> accountDiscounts = specialAuthService.getByAdAccountId(adAccountId);
            User user = tokenService.getUser(token);
            User userDB = userService.getByUserId(user.getUserId());
            UserDiscount userDiscounts = specialAuthService.getByIdNo(userDB.getAccountId(), userDB.getIdNO());
            if (userDiscounts != null) {
                map.put("auth", 1);
            }
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, accountDiscounts);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 添加特殊认证
     *
     * @param type        认证类型
     * @param authNo      证件号码
     * @param adAccountId 区域id
     * @param file        证件文件
     * @param token
     * @param session
     * @param request
     * @param response
     */
    @RequestMapping("/auth")
    public void auth(String authNo, Integer type, Integer adAccountId,
                     @RequestParam("file") MultipartFile file, String token, HttpSession session,
                     HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(authNo, "-200003", "请输入证件号码");

            if (file != null && file.isEmpty()) {
                throw new BaseException("-200004", "请上传证件照片");
            }
            User user = tokenService.getUser(token);
            specialAuthService.auth(user.getUserId(), type, authNo, adAccountId, file, user.getAccountId(), user.getPhone(), user.getName());
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取用户申请记录
     *
     * @param token
     * @param session
     * @param request
     * @param response
     */
    @RequestMapping("/getUserLog")
    public void getUserLog(String token, HttpSession session,
                           HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = tokenService.getUser(token);
            user = userService.getByUserId(user.getUserId());
            List<Integer> list = userService.getUserIdByIdNo(user.getIdNO(), user.getAccountId());
            UserAuthLog userAuthLog = null;
            log.info("特殊人群认证：idNO={},accountId={},idList={}", user.getIdNO(), user.getAccountId(), JSON.toJSONString(list));
            /* 校验 */
            for (Integer userId : list) {
                userAuthLog = specialAuthService.getAllUserLog(userId);
                if (userAuthLog != null) {
                    break;
                }
            }
            log.info("特殊人群认证：userAuthLog={}", JSON.toJSONString(userAuthLog));
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, userAuthLog);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

}
